using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace BitMango.Quest
{
	public class QuestManager : SingletonMonoBehaviour<QuestManager>
	{
		public static int AchievedCount
		{
			get
			{
				int num = 0;
				int i = 0;
				int count = QuestManager.Quests.Count;
				while (i < count)
				{
					if (QuestManager.Quests[i].State == STATE.ACHIEVED)
					{
						num++;
					}
					i++;
				}
				return num;
			}
		}

		public static int RewardedCount
		{
			get
			{
				int num = 0;
				int i = 0;
				int count = QuestManager.Quests.Count;
				while (i < count)
				{
					if (QuestManager.Quests[i].State == STATE.REWARDED)
					{
						num++;
					}
					i++;
				}
				return num;
			}
		}

		public static List<Quest> Quests
		{
			get
			{
				return SingletonMonoBehaviour<QuestManager>.Instance.quests;
			}
		}

		public static void Register(Quest quest)
		{
			SingletonMonoBehaviour<QuestManager>.Instance._Register(quest);
		}

		public static void Unregister(Quest quest)
		{
			SingletonMonoBehaviour<QuestManager>.Instance._Unregister(quest);
		}

		public static Quest GetQuestWithID(string id)
		{
			return SingletonMonoBehaviour<QuestManager>.Instance._GetQuestWithID(id);
		}

		public void _Register(Quest quest)
		{
			if (this.quests.Contains(quest))
			{
				return;
			}
			quest.OnChangeState += this.OnChangeState;
			if (quest.State == STATE.TRACKING)
			{
				quest.BeginTracking();
			}
			this.quests.Add(quest);
		}

		public void _Unregister(Quest quest)
		{
			if (!this.quests.Contains(quest))
			{
				return;
			}
			quest.OnChangeState -= this.OnChangeState;
			if (quest.State == STATE.TRACKING)
			{
				quest.EndTracking();
			}
		}

		public void ResetQuest()
		{
			if (this.questObj != null)
			{
				UnityEngine.Object.DestroyImmediate(this.questObj);
			}
			this.questObj = new GameObject("Quest");
			this.questObj.transform.parent = base.transform;
		}

		private void OnChangeState(Quest quest, STATE state)
		{
			if (state == STATE.ACHIEVED)
			{
				quest.EndTracking();
				this.ShowNotifiactionBanner(quest);
			}
			if (state == STATE.REWARDED)
			{
				this.GainRewardItem(quest);
			}
		}

		private Quest _GetQuestWithID(string id)
		{
			return this.quests.Find((Quest a) => a.ID.Equals(id));
		}

		private void ShowNotifiactionBanner(Quest quest)
		{
			this.displayQueue.Enqueue(quest);
			if (!this.isDisplayingBanner)
			{
				base.StartCoroutine(this.coShowNotifiactionBanner());
			}
		}

		private IEnumerator coShowNotifiactionBanner()
		{
			this.isDisplayingBanner = true;
			while (this.displayQueue.Count > 0)
			{
				Quest quest = this.displayQueue.Dequeue() as Quest;
				if (QuestManager.OnAchievedQuest != null)
				{
					yield return base.StartCoroutine(QuestManager.OnAchievedQuest(quest));
				}
			}
			this.isDisplayingBanner = false;
			yield break;
		}

		private void GainRewardItem(Quest quest)
		{
			Wallet.Gain(quest.RewardItemName, quest.RewardCount, null);
		}

		[HideInInspector]
		public GameObject questObj;

		public static Func<Quest, IEnumerator> OnAchievedQuest;

		private List<Quest> quests = new List<Quest>();

		private Queue displayQueue = new Queue();

		private bool isDisplayingBanner;
	}
}
